<template>
  <div class="find" style="width: 80%">
    <el-form
      :model="findForm"
      :rules="rules"
      ref="findForm"
      class="demo-ruleForm"
    >
      <el-form-item prop="password">
        <el-input
          clearable
          :type="flag ? 'text' : 'password'"
          v-model="findForm.password"
          autocomplete="off"
          placeholder="密码"
        >
          <i
            slot="suffix"
            :class="[flag ? 'el-icon-view' : 'el-icon-remove']"
            style="position: relative; right: 7px; top: 1px; font-size: 15px"
            @click="flag = !flag"
          />
        </el-input>
      </el-form-item>
      <el-form-item prop="checkPass">
        <el-input
          clearable
          type="password"
          v-model="findForm.checkPass"
          autocomplete="off"
          placeholder="确认密码"
        >
        </el-input>
      </el-form-item>
      <el-form-item class="reset">
        <el-button type="primary" class="verify" @click="submitForm('findForm')"
        @keyup.enter="submitForm('findForm')"
          >重 置</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "FindForm",
  props: ["email"],
  data() {
    var validatePass = (rule, value, callback) => {
      if (value == "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.checkPass != "") {
          this.$refs.findForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value == "") {
        callback(new Error("请再次输入密码"));
      } else if (value != this.findForm.password) {
        console.log(value);
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      //密码小眼睛切换状态
      flag: false,
      findForm: {
        userEmail: this.email,
        password: "",
      },
      rules: {
        password: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios({
            url: "/user/findPwd",
            method: "POST",
            data: JSON.stringify(this.findForm),
          })
            .then((res) => {
              console.log(res.data);
              if (res.data.status == 200) {
                this.$message.success(res.data.msg);
                setTimeout(() => {
                  this.$router.go(0);
                }, 2000);
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch(() => {
              this.$message.error("请求服务器错误");
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    /* resetForm(formName) {
      this.$refs[formName].resetFields();
    }, */
  },
};
</script>

<style scoped>
.findForm >>> .el-input input {
  height: 30px;
  border-radius: 10px;
}
.reset >>> .verify {
  position: absolute;
  right: 0;
  width: 35%;
  background-color: #39a7b0;
  color: #fff;
}
</style>
